package com.hice.demo.sbdbra.common.util;

import java.math.BigDecimal;

public class AmountUtils
{
  public static final String CURRENCY_FEN_REGEX = "\\-?[0-9]+";

  public static String changeF2YString(long amount)
    throws Exception
  {
    return BigDecimal.valueOf(amount).divide(new BigDecimal(100)).setScale(2, 4)
      .toString();
  }

  public static String changeF2Y(Long amount)
    throws Exception
  {
    if (!amount.toString().matches("\\-?[0-9]+")) {
      throw new Exception("金额格式有误");
    }

    int flag = 0;
    String amString = amount.toString();
    if (amString.charAt(0) == '-') {
      flag = 1;
      amString = amString.substring(1);
    }
    StringBuffer result = new StringBuffer();
    if (amString.length() == 1) {
      result.append("0.0").append(amString);
    } else if (amString.length() == 2) {
      result.append("0.").append(amString);
    } else {
      String intString = amString.substring(0, amString.length() - 2);
      for (int i = 1; i <= intString.length(); i++) {
        if (((i - 1) % 3 == 0) && (i != 1)) {
          result.append(",");
        }
        result.append(intString.substring(intString.length() - i, intString.length() - i + 1));
      }
      result.reverse().append(".").append(amString.substring(amString.length() - 2));
    }
    if (flag == 1) {
      return "-" + result.toString();
    }
    return result.toString();
  }

  public static String changeF2Y(String amount)
    throws Exception
  {
    if (!amount.matches("\\-?[0-9]+")) {
      throw new Exception("金额格式有误");
    }
    return BigDecimal.valueOf(Long.valueOf(amount).longValue()).divide(new BigDecimal(100))
      .setScale(2, 4)
      .toString();
  }

  public static String changeY2F(Long amount)
  {
    if (amount == null) {
      return "0";
    }
    return BigDecimal.valueOf(amount.longValue()).multiply(new BigDecimal(100)).toString();
  }

  public static String changeY2L(Long amount)
  {
    if (amount == null) {
      return "0";
    }
    return BigDecimal.valueOf(amount.longValue()).multiply(new BigDecimal(1000)).toString();
  }

  public static String changeL2Y(Long amount)
  {
    if (amount == null) {
      return new BigDecimal(0).setScale(2).toString();
    }
    return BigDecimal.valueOf(amount.longValue()).divide(new BigDecimal(1000)).setScale(2, 4).toString();
  }

  public static String changeL2M(Long amount)
  {
    if (amount == null) {
      return new BigDecimal(0).setScale(2).toString();
    }

    return BigDecimal.valueOf(amount.longValue()).divide(new BigDecimal(100)).setScale(2, 4).toString();
  }

  public static String changeY2F(String amount)
  {
    String currency = amount.replaceAll("\\$|\\￥|\\,", "");

    int index = currency.indexOf(".");
    int length = currency.length();
    Long amLong = Long.valueOf(0L);
    if (index == -1)
      amLong = Long.valueOf(currency + "00");
    else if (length - index >= 3)
      amLong = Long.valueOf(currency.substring(0, index + 3).replace(".", ""));
    else if (length - index == 2)
      amLong = Long.valueOf(currency.substring(0, index + 2).replace(".", "") + 0);
    else {
      amLong = Long.valueOf(currency.substring(0, index + 1).replace(".", "") + "00");
    }
    return amLong.toString();
  }

  public static Long roundedMoney(Long amount)
  {
    int value = 0;
    try {
      Float db = Float.valueOf(Float.valueOf(amount.toString()).floatValue() / 10.0F);
      value = Math.round(db.floatValue());
      value *= 10;
    } catch (Exception e) {
      value = 0;
    }
    return Long.valueOf(value);
  }

  public static String divide(String v1, Long v2, int scale)
  {
    if (scale < 0) {
      throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }
    BigDecimal b1 = new BigDecimal(v1);
    BigDecimal b2 = new BigDecimal(Double.toString(v2.longValue()));
    return b1.divide(b2, scale, 2).toString();
  }
}